﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace personalplaner.common.controls
{
	[TemplatePart(Name="PART_PinCollapse",Type=typeof(Button))]
	public class CollapsibleGroupBox : ContentControl
	{
		Button collapseButton;

		static CollapsibleGroupBox()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(CollapsibleGroupBox), new FrameworkPropertyMetadata(typeof(CollapsibleGroupBox)));
		}

		public override void OnApplyTemplate()
		{
			if (collapseButton != null)
			{
				collapseButton.Click -= new RoutedEventHandler(Button_Click);
				collapseButton = null;
			}

			base.OnApplyTemplate();

			collapseButton = GetTemplateChild("PART_PinCollapse") as Button;
			if(collapseButton!=null)
				collapseButton.Click += new RoutedEventHandler(Button_Click);
		}

		void Button_Click(object sender, RoutedEventArgs e)
		{
			if (IsCollapsed)
				IsCollapsed = false;
			else
				IsCollapsed = true;
		}

		#region Properties

		public string Title
		{
			get
			{
				return (string)GetValue(TitleProperty);
			}
			set
			{
				SetValue(TitleProperty, value);
			}
		}

		public static readonly DependencyProperty TitleProperty =
			DependencyProperty.Register("Title", typeof(string), typeof(CollapsibleGroupBox), new PropertyMetadata(""));

		public bool IsCollapsed
		{
			get
			{
				return (bool)GetValue(IsCollapsedProperty);
			}
			set
			{
				SetValue(IsCollapsedProperty, value);
			}
		}

		public static readonly DependencyProperty IsCollapsedProperty =
			DependencyProperty.Register("IsCollapsed", typeof(bool), typeof(CollapsibleGroupBox), new PropertyMetadata(false));

		public bool IsCollapseable
		{
			get
			{
				return (bool)GetValue(IsCollapseableProperty);
			}
			set
			{
				SetValue(IsCollapseableProperty, value);
			}
		}

		public static readonly DependencyProperty IsCollapseableProperty =
			DependencyProperty.Register("IsCollapseable", typeof(bool), typeof(CollapsibleGroupBox), new PropertyMetadata(true));

		public CornerRadius CornerRadius
		{
			get
			{
				return (CornerRadius)GetValue(CornerRadiusProperty);
			}
			set
			{
				SetValue(CornerRadiusProperty, value);
			}
		}

		public static readonly DependencyProperty CornerRadiusProperty =
			DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(CollapsibleGroupBox), new PropertyMetadata(new CornerRadius(0, 0, 5, 5)));

		public CornerRadius HeaderCornerRadius
		{
			get
			{
				return (CornerRadius)GetValue(HeaderCornerRadiusProperty);
			}
			set
			{
				SetValue(HeaderCornerRadiusProperty, value);
			}
		}

		public static readonly DependencyProperty HeaderCornerRadiusProperty =
			DependencyProperty.Register("HeaderCornerRadius", typeof(CornerRadius), typeof(CollapsibleGroupBox), new PropertyMetadata(new CornerRadius(5, 5, 0, 0)));

		public Thickness HeaderBorderThickness
		{
			get
			{
				return (Thickness)GetValue(HeaderBorderThicknessProperty);
			}
			set
			{
				SetValue(HeaderBorderThicknessProperty, value);
			}
		}

		public static readonly DependencyProperty HeaderBorderThicknessProperty =
			DependencyProperty.Register("HeaderBorderThickness", typeof(Thickness), typeof(CollapsibleGroupBox), new PropertyMetadata(new Thickness(1, 1, 1, 1)));

		public Brush HeaderBackground
		{
			get
			{
				return (Brush)GetValue(HeaderBackgroundProperty);
			}
			set
			{
				SetValue(HeaderBackgroundProperty, value);
			}
		}

		public static readonly DependencyProperty HeaderBackgroundProperty =
			DependencyProperty.Register("HeaderBackground", typeof(Brush), typeof(CollapsibleGroupBox), new PropertyMetadata(Brushes.Gray));

		#endregion
	}
}
